package com.asurplus.websocket.controller;

import com.asurplus.common.shiro.ShiroUtils;
import com.asurplus.common.utils.R;
import com.asurplus.websocket.config.WebSocketServer;
import com.asurplus.websocket.vo.WebsocketVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lizhou
 * @since 2020-10-13
 **/
@Api(tags = "后台管理端--websocket")
@Controller
@RequestMapping("/websocket")
public class WebsocketController {

    @Value("${server.servlet.context-path}")
    public String contextPath;
    @Autowired
    private WebSocketServer webSocketServer;

    @ApiOperation(value = "请求websocket页面")
    @GetMapping("init")
    public String init(Model model) {
        model.addAttribute("url", "ws://${host}" + contextPath + "/websocket/" + ShiroUtils.getSysUserId());
        return "websocket/info";
    }

    @PostMapping("sendMessage")
    @ResponseBody
    public R sendMessage(@RequestBody WebsocketVO websocketVO) {
        webSocketServer.sendMessage(websocketVO.getContent(), websocketVO.getReceiveUser());
        return R.ok();
    }

}
